import { HYDRATE } from "next-redux-wrapper";
import { AuthTypes } from "redux/constants";

const initialState = {
  isAuth: false,
  loading: false,
  error: null,
};

export default function (state = initialState, action) {
  switch (action.type) {
    case HYDRATE:
      return { ...state, ...action.payload };
    case AuthTypes.GET_SUCCESS:
      return {
        ...action.payload,
        isAuth: true,
        loading: false,
        error: null,
      };
    case AuthTypes.CHECK_AUTH:
      return {
        ...action.payload,
        isAuth: false,
        loading: true,
        error: null,
      };
    case AuthTypes.GET_FAILURE:
      return {
        isAuth: false,
        loading: false,
        error: action.payload,
      };
    case AuthTypes.GET_LOGOUT_REQUEST:
      return { ...state, loading: true, err: null };
    case AuthTypes.GET_LOGOUT_SUCCESS:
      return initialState;
    default:
      return state;
  }
}
